package com.zc.operator.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.zc.iotDevice.mapper.ChargeStationPileMapper;
import com.zc.operator.domain.ChargeStation;
import com.zc.operator.domain.ChargeStationAdjustPriceAdditional;
import com.zc.operator.domain.ChargeStationPriceType;
import com.zc.operator.mapper.ChargeStationMapper;
import com.zc.operator.mapper.ChargeStationPriceTypeMapper;
import com.zc.remoteControl.service.RemoteInstructSendService;
import com.zc.trendsDispatch.domain.ChargeRuleTrigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zc.operator.mapper.ChargeStationAdjustPriceMapper;
import com.zc.operator.domain.ChargeStationAdjustPrice;
import com.zc.operator.service.IChargeStationAdjustPriceService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 调整电价Service业务层处理
 *
 * @author glb
 * @date 2024-03-12
 */
@Service
public class ChargeStationAdjustPriceServiceImpl implements IChargeStationAdjustPriceService {
    @Autowired
    private ChargeStationAdjustPriceMapper chargeStationAdjustPriceMapper;

    @Resource
    private RemoteInstructSendService remoteInstructSendService;

    @Resource
    private ChargeStationMapper chargeStationMapper;

    @Resource
    private ChargeStationPriceTypeMapper chargeStationPriceTypeMapper;

    @Resource
    private ChargeStationPileMapper chargeStationPileMapper;

    /**
     * 查询调整电价
     *
     * @param id 调整电价主键
     * @return 调整电价
     */
    @Override
    public ChargeStationAdjustPrice selectChargeStationAdjustPriceById(Long id) {
        return chargeStationAdjustPriceMapper.selectChargeStationAdjustPriceById(id);
    }

    @Override
    public ChargeStationAdjustPrice selectChargeStationAdjustPriceByStationCode(ChargeStationAdjustPrice chargeStationAdjustPrice) {
        List<ChargeStationAdjustPrice> chargeStationAdjustPrices = chargeStationAdjustPriceMapper.selectChargeStationAdjustPriceList(chargeStationAdjustPrice);
        if (chargeStationAdjustPrices.size() > 0) {
            ChargeStationAdjustPrice chargeStationAdjustPrice1 = chargeStationAdjustPrices.get(0);
            if ("1".equals(chargeStationAdjustPrice1.getPricing())) {
                List<ChargeStationAdjustPriceAdditional> ruleJSONObject = new ArrayList<>();
                ChargeStationAdjustPriceAdditional chargeStationAdjustPriceAdditional = new ChargeStationAdjustPriceAdditional();
                chargeStationAdjustPriceAdditional.setInterval("[\"00:00\",\"24:00\"]");
                chargeStationAdjustPriceAdditional.setPeriod("全天同价");
                chargeStationAdjustPriceAdditional.setPeriodNum(6);
                chargeStationAdjustPriceAdditional.setElectricityPrice(Double.parseDouble(chargeStationAdjustPrice1.getElectricPrice()));
                chargeStationAdjustPriceAdditional.setServiceCharge(Double.parseDouble(chargeStationAdjustPrice1.getServicePrice()));
                chargeStationAdjustPriceAdditional.setPlus("+");
                chargeStationAdjustPriceAdditional.setUnit("元/度");
                chargeStationAdjustPriceAdditional.setCol("#1d58a9");
                ruleJSONObject.add(chargeStationAdjustPriceAdditional);
                chargeStationAdjustPrice1.setTableDataList(ruleJSONObject);
                Map map = new HashMap();
                map.put("same", "[[0,48]]");
                chargeStationAdjustPrice1.setDateFromMap(map);
            } else {
                List<ChargeStationAdjustPriceAdditional> ruleJSONObject = JSONObject.parseArray(chargeStationAdjustPrice1.getTableData(), ChargeStationAdjustPriceAdditional.class);
                chargeStationAdjustPrice1.setTableDataList(ruleJSONObject);
                JSONObject dateFrom = JSONObject.parseObject(chargeStationAdjustPrice1.getDateFrom());
                Map map = new HashMap();
                map.put("tip", String.valueOf(dateFrom.get("tip")));
                map.put("peak", String.valueOf(dateFrom.get("peak")));
                map.put("flat", String.valueOf(dateFrom.get("flat")));
                map.put("valley", String.valueOf(dateFrom.get("valley")));
                map.put("deepValley", String.valueOf(dateFrom.get("deepValley")));
                chargeStationAdjustPrice1.setDateFromMap(map);
            }
            return chargeStationAdjustPrice1;
        } else {
            return null;
        }

    }

    /**
     * 查询调整电价列表
     *
     * @param chargeStationAdjustPrice 调整电价
     * @return 调整电价
     */
    @Override
    public List<ChargeStationAdjustPrice> selectChargeStationAdjustPriceList(ChargeStationAdjustPrice chargeStationAdjustPrice) {
        return chargeStationAdjustPriceMapper.selectChargeStationAdjustPriceList(chargeStationAdjustPrice);
    }

    /**
     * 新增调整电价
     *
     * @param chargeStationAdjustPrice 调整电价
     * @return 结果
     */
    @Override
    public int insertChargeStationAdjustPrice(ChargeStationAdjustPrice chargeStationAdjustPrice) {
        chargeStationAdjustPrice.setCreateBy(getUsername());
        chargeStationAdjustPrice.setCreateTime(DateUtils.getNowDate());
        distributeBillingTemplate(chargeStationAdjustPrice);
        return chargeStationAdjustPriceMapper.insertChargeStationAdjustPrice(chargeStationAdjustPrice);
    }

    /**
     * 修改调整电价
     *
     * @param chargeStationAdjustPrice 调整电价
     * @return 结果
     */
    @Override
    public int updateChargeStationAdjustPrice(ChargeStationAdjustPrice chargeStationAdjustPrice) {
        chargeStationAdjustPrice.setUpdateBy(getUsername());
        chargeStationAdjustPrice.setUpdateTime(DateUtils.getNowDate());
        distributeBillingTemplate(chargeStationAdjustPrice);
        return chargeStationAdjustPriceMapper.updateChargeStationAdjustPrice(chargeStationAdjustPrice);
    }

    //下发价格至关联设备
    private void distributeBillingTemplate(ChargeStationAdjustPrice chargeStationAdjustPrice) {
        //查询所有设备
        ChargeStationPriceType chargeStationPriceType = chargeStationPriceTypeMapper.selectChargeStationPriceTypeByPriceTypeId(chargeStationAdjustPrice.getPriceTypeId());
        ChargeStation chargeStation = new ChargeStation();
        chargeStation.setPriceTypeCode(chargeStationPriceType.getPriceTypeCode());
        List<ChargeStation> chargeStations = chargeStationMapper.selectChargeStationList(chargeStation);
        if (chargeStations.size() > 0) {
            List<String> deviceIdList = new ArrayList<>();
            for (ChargeStation station : chargeStations) {
                List<String> idList = chargeStationPileMapper.getDeviceIdList(station.getChargeStationCode());
                deviceIdList.addAll(idList);
            }
            //下发价格
            Map<String, Object> stringObjectMap =
                    remoteInstructSendService.distributeBillingTemplate(deviceIdList, chargeStationAdjustPrice.getPriceTypeId(), "YTSN-CDZ-N01");
        }

    }

    /**
     * 批量删除调整电价
     *
     * @param ids 需要删除的调整电价主键
     * @return 结果
     */
    @Override
    public int deleteChargeStationAdjustPriceByIds(Long[] ids) {
        return chargeStationAdjustPriceMapper.deleteChargeStationAdjustPriceByIds(ids);
    }

    /**
     * 删除调整电价信息
     *
     * @param id 调整电价主键
     * @return 结果
     */
    @Override
    public int deleteChargeStationAdjustPriceById(Long id) {
        return chargeStationAdjustPriceMapper.deleteChargeStationAdjustPriceById(id);
    }
}
